define(function(require,exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var popwin = require('@base/dialog').base;
	var grid = require('@base/highGrid');
	var util = require('util');

	// 结果界面
	var RefStaffResult = popwin.extend({
		init: function(config){
			config = pubjs.conf(config,{
				'title': LANG('关联账号'),
				'width': 600,
				'id': null,
				'updateUrl': 'qualification/relateEmployee',
				'vModel':{

				}
			});

			this.$ids = null;
			this.$id = null;

			this.Super('init',arguments);
		},
		afterBuild:function(){
			this.Super('afterBuild',arguments);
			var el = this.getDOM('body');
			el.append($([
				'<button class="btn btn-primary mb10 addStaff">'+ LANG('添加关联账号') +'</button>',
				'<div pub-name="grid" pub-mod="admin/aptitude/grid/staff.base"></div>'
			].join('')));

			this.buildFromTemplate({
				'grid': {
					// 'url': '',
					'param': {

					},
					'hasSelect': false,
					'eventDataLoad': true,
					'auto_load': false,
					'operation': {
						'render': 'renderOperation',
						'cls': 'M-gridOPCursor',
						'width': 80
					}
				}
			});

			this.uiBind(el.find('.addStaff'), 'click', this.eventAddStaff);

			this.vm.mount();

		},
		// 渲染操作列
		renderOperation: function(i, val, data, con){
			var html = '';

			html = [
				'<a data-op="remove" data-id="'+data.id+'">'+LANG("删除")+'</a>'
				// '<a data-op="edit" data-id="'+data.id+'">'+LANG("编辑")+'</a>',
				// '<a data-op="copy" data-id="'+data.id+'">'+LANG("复制")+'</a>'
			];

			return html.join(' | ');
		},
		onListOpClick: function(ev){
			var index = ev.param.index;
			switch (ev.param.op){
				case 'remove':
					this.$ids.splice(index, 1);
					this.$.grid.setParam({'ids': this.$ids}).load();
					break;
			}
		},
		eventAddStaff: function(evt, elm){
			var addStaff = this.get('addStaff');
			var ids = this.$ids;
			if(!addStaff){
				this.createAsync('addStaff','admin/aptitude/popwin/refStaff.refStaffAdd',function(mod){
					mod.reset().setData(ids).show();
				});
			}else{
				addStaff.reset().setData(ids).show();
			}
			return this;
		},
		onOk: function(ev){
			var self = this;
			var c = self.getConfig();

			self.showLoading();
			pubjs.mc.send(c.updateUrl, {
				'_id': self.$id,
				'Employee': this.$ids
			}, self.afterSave.bind(self));
		},
		afterSave: function(err, data){
			this.hideLoading();
			if (err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert(LANG('关联成功'));
			this.fire('selectStaffSuccess', {});
			this.hide();
		},
		setData: function(data){
			this.$id = data._id;
			this.$.grid.setParam({'ids': data.RelateEmployee}, true).load();
			this.$ids = data.RelateEmployee;
			return this;
		},
		onAddStaffSuccess: function(ev){
			if(ev && ev.param){
				this.$ids = ev.param;
				this.$.grid.setParam({ids: ev.param}).reload();
			}
		},
		reset: function(){
			this.vm.reset();
			this.$id = null;
			this.$ids = null;
			this.$.grid.reload();
			return this;
		}
	});

	exports.main = RefStaffResult;

	// 选择界面
	var RefStaffAdd = popwin.extend({
		init: function(config){
			config = pubjs.conf(config,{
				'title': LANG('关联账号'),
				'width': 600,
				'id': null,
				'vModel':{

				}
			});

			this.Super('init',arguments);
		},
		afterBuild:function(){
			this.Super('afterBuild',arguments);
			var el = this.getDOM('body');
			el.append($([
				'<span class="dn" pub-name="addButton" pub-mod="@base/common/input.button"></span>',
				'<div pub-name="grid" pub-mod="admin/aptitude/grid/staff.base"></div>'
			].join('')));

			this.buildFromTemplate({
				'addButton': {
					'value': LANG('添加关联账号'),
					'class': "btn btn-primary mb5",
					'width': 120
				}
			});

		},
		onOk: function(ev){
			this.fire('addStaffSuccess', this.$.grid.getSelectRowIds());
			this.hide();
		},
		setData: function(ids){
			this.$.grid.setSelectRowIds(ids);
			return this;
		},
		reset: function(){
			this.vm.reset();
			this.$.grid.setSelectRowIds([]);
			return this;
		}
	});

	exports.refStaffAdd = RefStaffAdd;
})